package cn.cyb.server.security;

import cn.cyb.server.common.constant.HttpStatus;
import cn.cyb.server.common.exception.ServiceException;
import cn.cyb.server.domain.model.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author Administrator
 * @date 2025/1/10 0010
 */
public class SecurityUtils {

    public static LoginUser getLoginUser(){
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            return loginUser;
        } catch (Exception e) {
            throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
        }
    }
}
